﻿using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using System.Text;

/*
保存数据
Dictionary<int, List<int>> mySlotData = new Dictionary<int, List<int>>();
mySlotData.Add(1, new List<int> { true, false, true });
SlotDataManager.SaveSlotData(mySlotData);
读取数据
Dictionary<int, List<int>> loadedData = SlotDataManager.LoadSlotData();
if (loadedData.ContainsKey(1))
{
    List<int> level1Data = loadedData[1];
    int isLevel1Completed = level1Data[0]; // 第一个int表示是否通关
}
*/

public class SlotDataManager : MonoBehaviour
{
    private static string SavePath => Application.persistentDataPath + "/slotData.json";

    // 将字典转换为可序列化的结构
    public List<SlotDataEntry> entries = new List<SlotDataEntry>();

    //保存数据到本地
    public static void SaveSlotData(Dictionary<int, List<int>> slotLists)
    {
        SlotDataContainer container = new SlotDataContainer();
        container.Slot_lists = slotLists;
        string jsonData = JsonUtility.ToJson(container, true);
        //string jsonData = "";
        //foreach (int key in container.Slot_lists.Keys)
        //{
        //    jsonData += key + ":";
        //    for (int i = 0; i < container.Slot_lists[key].Count; i++)
        //    {
        //        if (i != 0)
        //        {
        //            jsonData += ",";
        //        }
        //        jsonData += container.Slot_lists[key][i];
        //    }
        //    jsonData += "|";
        //}


        // 2. 使用StringBuilder提高字符串拼接性能
        StringBuilder jsonBuilder = new StringBuilder();

        foreach (var kvp in slotLists)
        {
            jsonBuilder.Append(kvp.Key).Append(":");
            jsonBuilder.Append(string.Join(",", kvp.Value));
            jsonBuilder.Append("|");
        }


        File.WriteAllText(SavePath, jsonBuilder.ToString());//

        Debug.Log("数据已保存到: " + SavePath);

    }

    // 从本地读取数据
    public static Dictionary<int, List<int>> LoadSlotData()
    {
        if (File.Exists(SavePath))
        {
            string jsonData = File.ReadAllText(SavePath);
            //SlotDataContainer container = JsonUtility.FromJson<SlotDataContainer>(jsonData);
            Dictionary<int, List<int>> Slot_lists = new Dictionary<int, List<int>>();

            string[] str = jsonData.Split('|');
            for(int i=0;i<str.Length;i++)
            {
                if(str[i] != "")
                {
                    string[] str1 = str[i].Split(':');
                    string[] str2 = str1[1].Split(',');
                    for(int j=0;j<str2.Length;j++)
                    {
                        if(Slot_lists.ContainsKey(int.Parse(str1[0])))
                        {
                            Slot_lists[int.Parse(str1[0])].Add(int.Parse(str2[j]));
                        }
                        else
                        {
                            Slot_lists.Add(int.Parse(str1[0]), new List<int>());
                            Slot_lists[int.Parse(str1[0])].Add(int.Parse(str2[j]));
                        }
                    }
                }
            }
            Debug.Log("数据已从 " + SavePath + " 加载");
            return Slot_lists;
        }

        Debug.Log("未找到保存文件，创建新数据");
        return new Dictionary<int, List<int>>();
    }

    // 添加从字典转换的方法
    public void FromDictionary(Dictionary<int, List<int>> dict)
    {
        entries.Clear();
        foreach (var pair in dict)
        {
            entries.Add(new SlotDataEntry
            {
                key = pair.Key,
                values = pair.Value
            });
        }
    }
    // 转换回字典
    public Dictionary<int, List<int>> ToDictionary()
    {
        var dict = new Dictionary<int, List<int>>();
        foreach (var entry in entries)
        {
            dict.Add(entry.key, entry.values);
        }
        return dict;
    }

}


[Serializable]
public class SlotDataEntry
{
    public int key;
    public List<int> values;
}

[Serializable]
public class SlotDataContainer
{
    public Dictionary<int, List<int>> Slot_lists = new Dictionary<int, List<int>>();

    // 添加数据的方法
    public void AddSlotData(int key, List<int> values)
    {
        if (Slot_lists.ContainsKey(key))
        {
            Slot_lists[key] = values;
        }
        else
        {
            Slot_lists.Add(key, values);
        }
    }

    // 获取数据的方法
    public List<int> GetSlotData(int key)
    {
        if (Slot_lists.ContainsKey(key))
        {
            return Slot_lists[key];
        }
        return null;
    }
}